package controllers

import (
    "gin-worked/app/system/service"
    "gin-worked/app/system/validate"
    "gin-worked/work"
    "github.com/gin-gonic/gin"
)

type RoleController struct {
}

func (r RoleController) List(c *gin.Context) {
    var list validate.RoleList

    if err := c.ShouldBind(&list); err != nil {
        work.ReturnResult(c, 0, err.Error(), "")
        return
    }

    param := c.MustGet("params").(map[string]string)

    res := service.RoleList(param["Limit"], param["Offset"])
    work.ReturnResult(c, res.Code, res.Msg, res.Data)
    return
}

// Add 添加角色
func (r RoleController) Add(c *gin.Context) {
    var add validate.RoleAdd

    if err := c.ShouldBind(&add); err != nil {
        work.ReturnResult(c, 0, err.Error(), "")
        return
    }
    param := c.MustGet("params").(map[string]string)

    res := service.RoleAdd(param["Name"], param["Sort"], param["Status"], param["IsAdmin"])
    work.ReturnResult(c, res.Code, res.Msg, res.Data)
    return
}

// Info 角色详情
func (r RoleController) Info(c *gin.Context) {
    var info validate.RoleInfo

    if err := c.ShouldBind(&info); err != nil {
        work.ReturnResult(c, 0, err.Error(), "")
        return
    }

    param := c.MustGet("params").(map[string]string)
    res := service.RoleInfo(param["Id"])
    work.ReturnResult(c, res.Code, res.Msg, res.Data)
    return
}

// Edit Add 编辑角色
func (r RoleController) Edit(c *gin.Context) {
    var edit validate.RoleEdit

    if err := c.ShouldBind(&edit); err != nil {
        work.ReturnResult(c, 0, err.Error(), "")
        return
    }
    param := c.MustGet("params").(map[string]string)

    res := service.RoleEdit(param["Id"], param["Name"], param["Sort"], param["Status"], param["IsAdmin"])
    work.ReturnResult(c, res.Code, res.Msg, res.Data)
    return
}

// Del Info 角色删除
func (r RoleController) Del(c *gin.Context) {
    var info validate.RoleInfo

    if err := c.ShouldBind(&info); err != nil {
        work.ReturnResult(c, 0, err.Error(), "")
        return
    }

    param := c.MustGet("params").(map[string]string)
    res := service.RoleDel(param["Id"])
    work.ReturnResult(c, res.Code, res.Msg, res.Data)
    return
}
